package util;

/**
 * Created by Tecnocom on 1/18/2015.
 */
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;

import com.gonfre.quientelo.MyActivity;
import com.gonfre.quientelo.R;
import com.gonfre.quientelo.RegisterSellerActivity;
import com.gonfre.quientelo.RequestPriceActivity;

public class CustomAutoCompleteTextChangedListener implements TextWatcher{

    Context context;

    int secTexto;

    /*public CustomAutoCompleteTextChangedListener(Context context){
        this.context = context;
    }*/

    public CustomAutoCompleteTextChangedListener(Context context, int secTexto){
        this.context = context;
        this.secTexto = secTexto;
    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        // TODO Auto-generated method stub

    }

    /*
    @Override
    public void onTextChanged(CharSequence userInput, int start, int before, int count) {

        try{

            MyActivity mainActivity = ((MyActivity) context);

            // update the adapater
            mainActivity.myAdapter.notifyDataSetChanged();

            // get suggestions from the database
            String query_item = mainActivity.getString(R.string.query_item_ubicacion);
            MyDBObject[] myObjs = mainActivity.dbLocations.readItem(query_item, userInput.toString(), "ubicacion", "id");

            // update the adapter
            mainActivity.myAdapter = new AutocompleteCustomArrayAdapter(mainActivity, R.layout.list_view_row, myObjs, userInput.toString());

            mainActivity.myAutoComplete.setAdapter(mainActivity.myAdapter);

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    */

    @Override
    public void onTextChanged(CharSequence userInput, int start, int before, int count) {

        try{

            MyActivity mainActivity = ((MyActivity) context);

            // update the adapater
            mainActivity.myAdapters.get(secTexto).notifyDataSetChanged();

            // get suggestions from the database
            String query_item = mainActivity.getString(R.string.query_item_ubicacion);
            MyDBObject[] myObjs = mainActivity.dbLocations.readItem(query_item, userInput.toString(), "ubicacion", "id");

            // update the adapter
            mainActivity.myAdapters.set(secTexto, new AutocompleteCustomArrayAdapter(mainActivity, R.layout.list_view_row, myObjs, userInput.toString()));

            mainActivity.myAutoCompletes.get(secTexto).setAdapter(mainActivity.myAdapters.get(secTexto));

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
